<?php
/**
 * DouPHP小程序模块
 * Author: cuihaifeng  qq 366131726 微信同号
 * 网址：www.zbxingke.com www.0533hf.com
 */


define('IN_DOUCO', true);

require(dirname(__FILE__) . '/include/init.php');
include_once(ROOT_PATH . 'include/upload.class.php');

// rec操作项的初始化
$rec = $check->is_rec($_REQUEST['rec']) ? $_REQUEST['rec'] : 'system';

// 赋值给模板
$smarty->assign('rec', $rec);
$smarty->assign('cur', 'wxapp');

/**
 * +----------------------------------------------------------
 * 系统设置
 * +----------------------------------------------------------
 */
if ($rec == 'system') {
}
/**
 * +----------------------------------------------------------
 * 自定义导航
 * +----------------------------------------------------------
 */
elseif ($rec == 'nav') {

}

/**
 * +----------------------------------------------------------
 * 首页幻灯图片
 * +----------------------------------------------------------
 */
elseif ($rec == 'show') {

}


/**
 * +----------------------------------------------------------
 * 首页全屏广告图
 * +----------------------------------------------------------
 */
elseif ($rec == 'full_ad') {

}


/**
 * +----------------------------------------------------------
 * 设置模板
 * +----------------------------------------------------------
 */
elseif ($rec == 'theme') {
    $smarty->assign('ur_here', $_LANG['mobile_theme']);

    // act操作项的初始化
    $act = $check->is_rec($_REQUEST['act']) ? $_REQUEST['act'] : 'default';

    // 赋值给模板
    $smarty->assign('act', $act);

    // 幻灯列表
    if ($act == 'default') {
        $theme_enable = $dou->get_theme_info($_CFG['mobile_theme'], true);
        $theme_array = $dou->get_subdirs(ROOT_PATH . M_PATH . '/theme/');
        foreach ($theme_array as $unique_id) {
            if ($unique_id == $_CFG['mobile_theme']) continue;
            $theme_list[] = $dou->get_theme_info($unique_id, true);
        }

        // 赋值给模板
        $smarty->assign('theme_enable', $theme_enable);
        $smarty->assign('theme_list', $theme_list);

        $smarty->display('mobile.htm');
    } // 在线安装模板
    elseif ($act == 'install') {
        $smarty->assign('get', urlencode(serialize($_GET)));
        $smarty->assign('localsite', $dou->dou_localsite('mobile'));

        $smarty->display('mobile.htm');
    } // 模板启用
    elseif ($act == 'enable') {
        if ($check->is_extend_id($unique_id = $_REQUEST['unique_id'])) {
            $theme_array = $dou->get_subdirs(ROOT_PATH . M_PATH . '/theme/');
            if (in_array($unique_id, $theme_array)) { // 判断删除操作的模板是否真实存在
                // 替换系统设置中模板值
                $dou->query("UPDATE " . $dou->table('config') . " SET value = '$unique_id' WHERE name = 'mobile_theme'");
                $dou->dou_clear_cache(ROOT_PATH . 'cache/m'); // 更新缓存
            }
        }

        $dou->dou_header('mobile.php?rec=theme');
    } // 删除模板
    elseif ($act == 'del') {
        // 载入扩展功能
        include_once(ROOT_PATH . ADMIN_PATH . '/include/cloud.class.php');
        $dou_cloud = new Cloud('cache');

        if ($check->is_extend_id($unique_id = $_REQUEST['unique_id'])) {
            $theme_array = $dou->get_subdirs(ROOT_PATH . M_PATH . '/theme/');
            if (in_array($unique_id, $theme_array)) { // 判断删除操作的模板是否真实存在
                $dou->del_dir(ROOT_PATH . M_PATH . '/theme/' . $unique_id);
                $dou_cloud->change_updatedate('mobile', $unique_id, true); // 删除更新时间记录
                $dou->create_admin_log($_LANG['mobile_theme_del'] . ': ' . $unique_id);
            }
        }

        $dou->dou_header('mobile.php?rec=theme');
    }
}

$file = ROOT_PATH.ADMIN_PATH."/wxapp/{$rec}.ctrl.php";

if(file_exists($file)){
    require_once $file;
}

/**
 * +----------------------------------------------------------
 * 获取系统设置列表
 * +----------------------------------------------------------
 */
function get_cfg_list()
{
    $sql = "SELECT * FROM " . $GLOBALS['dou']->table('config') . " WHERE tab = 'wxapp' ORDER BY sort ASC";
    $query = $GLOBALS['dou']->query($sql);
    while ($row = $GLOBALS['dou']->fetch_array($query)) {
        if ($row['box']) {
            $box = explode(",", $row['box']);
        }
        if ($row['name'] == 'mobile_logo') {
            $row['value'] = $row['value'] ? M_PATH . '/theme/' . $GLOBALS['_CFG']['mobile_theme'] . '/images/' . $row['value'] : '';
        }

        $cue = $GLOBALS['_LANG'][$row['name'] . '_cue'];

        // 数组类型的设置选项
        if ($row['type'] == 'array') {
            $arr = unserialize($row['value']);
            foreach ((array)$arr as $key => $v) {
                $value_array[] = array(
                    "value" => $v,
                    "name" => $row['name'] . '[' . $key . ']',
                    "lang" => $GLOBALS['_LANG'][$row['name'] . '_' . $key],
                    "cue" => $GLOBALS['_LANG'][$row['name'] . '_' . $key . '_cue']
                );
            }
        }

        $cfg_list[] = array(
            "value" => $value_array ? $value_array : $row['value'],
            "name" => $row['name'],
            "type" => $row['type'],
            "box" => $box,
            "lang" => $GLOBALS['_LANG'][$row['name']],
            "cue" => $cue
        );
    }

    return $cfg_list;
}

?>